package com.xiaoshuidi.cloud.module.member.service.programarticle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.ProgramCreateVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.ProgramPageVO;
import com.xiaoshuidi.cloud.module.member.controller.admin.programarticle.vo.ProgramRespVO;
import com.xiaoshuidi.cloud.module.member.mapper.programarticle.MemberProgramInfoMapper;
import com.xiaoshuidi.cloud.module.member.pojo.programarticle.MemberProgramInfo;
import com.xiaoshuidi.cloud.module.member.repository.MemberProgramInfoRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class MemberProgramInfoServiceImpl extends ServiceImpl<MemberProgramInfoMapper, MemberProgramInfo> implements MemberProgramInfoService {
    private final MemberProgramInfoRepository memberProgramInfoRepository;

    @Override
    public Boolean create(ProgramCreateVO vo) {
        memberProgramInfoRepository.create(vo);
        return true;
    }

    @Override
    public PageResult<ProgramRespVO> getPage(ProgramPageVO pageVo) {
        Page<MemberProgramInfo> page = memberProgramInfoRepository.getPage(pageVo);
        PageResult<ProgramRespVO> result = PageResult.empty();
        result.setTotal(page.getTotal());
        List<ProgramRespVO> collect = page.getRecords().stream().map(p -> {
            ProgramRespVO programPageVO = new ProgramRespVO();
            BeanUtils.copyProperties(p, programPageVO);
            return programPageVO;
        }).collect(Collectors.toList());
        result.setList(collect);
        return result;
    }

    @Override
    public Boolean edit(ProgramCreateVO vo) {
        MemberProgramInfo info = new MemberProgramInfo();
        BeanUtils.copyProperties(vo, info);
        memberProgramInfoRepository.updateById(info);
        return true;
    }
}
